<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer';

export default {
  name: "OperationLog",
  components: {ElImageViewer},
  data() {
    return {
      ip: null,
      operation: null,
      operator: null,
      type: null,
      status: null,
      dateRange:'',
      startTime:'',
      endTime:'',
      tableData: [],
      log:{},
      currentPage: 1,
      pageSize: 5,
      total: 0,
      visible: false,
      loading: true,
      multipleSelection: [],
      dialogVisible: false,
      formLabelWidth: '70px',
      statusOptions:[{
        value:'0',
        label:'失败'
      },{
        value:'1',
        label:'成功'
      }],
      typeOptions:[{
        value:'新增',
        label:'新增'
      },{
        value:'更新',
        label:'更新'
      },{
        value:'删除',
        label:'删除'
      },{
        value:'批量删除',
        label:'批量删除'
      },{
        value:'清空全部',
        label:'清空全部'
      },{
        value:'登录',
        label:'登录'
      },{
        value:'注册',
        label:'注册'
      }]
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      if(this.dateRange==null){
        this.startTime=null;
        this.endTime=null;
      }else{
        this.startTime= this.startTime=this.dateRange.toString().substring(0,19);
        this.endTime=this.dateRange.toString().substring(20);
      }
      this.request.get("/operation-log/page", {
        params: {
          ip: this.ip,
          operation: this.operation,
          operator: this.operator,
          type: this.type,
          status: this.status,
          startTime: this.startTime,
          endTime: this.endTime,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
        }
      }).then(res => {
        this.tableData = res.data.records;
        this.loading = false;
        this.total = res.data.total;
      });

    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();

    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.load();
    },
    reset() {
      this.ip = null;
      this.operation = null;
      this.operator = null;
      this.type = null;
      this.dateRange=null;
      this.status = null;
      this.tableData = [];
      this.currentPage = 1;
      this.pageSize = 5;
      this.total = 0;
      this.load();
    },
    viewLog(log){
      this.log=log;
      this.dialogVisible=true;
    },
    handleDelete(id) {
      this.request.delete("/operation-log/delete/" + id).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((a) => a.id);
      console.log(this.multipleSelection);
    },
    handleBatchRemove() {
      let ids = this.multipleSelection;
      this.request.post("/operation-log/deleteBatch", ids).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    handleRemoveAll() {
      this.request.get("/operation-log/deleteAll").then((res) => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    filterStatus(value, row) {
      return row.status === value;
    },
    headerBg({row, rowIndex}) {
      if (rowIndex == 0) {
        return 'headerBgClass';
      }
    },
    exp() {
      window.open(this.baseUrl+'/user/export')
      // this.request.get("/user/export",{
      //   responseType: 'blob'
      // }).then((res)=>{
      //    if(!res){
      //      return;
      //    }
      //   let blob;
      //
      //   let fileName=window.decodeURI(res.headers['content-disposition'].split('=')[1]);
      //   let url = window.URL.createObjectURL(blob);
      //   let aLink = document.createElement("a");
      //   aLink.style.display = "none";
      //   aLink.href = url;
      //   aLink.setAttribute("download", fileName);
      //   document.body.appendChild(aLink);
      //   aLink.click();
      //   document.body.removeChild(aLink);
      //   window.URL.revokeObjectURL(url);
      //   return this.$message.success("导出成功");
      // })
    }
  }

}
</script>

<template>
  <div>
    <div>
      <div>
        <el-input suffix-icon="el-icon-search" placeholder="请输入操作地址" v-model="ip"
                  style="width:160px"></el-input>
        <el-input suffix-icon="el-icon-search" placeholder="请输入系统模块" v-model="operation"
                  style="margin-left: 5px;width:160px"></el-input>
        <el-input suffix-icon="el-icon-search" placeholder="请输入操作人员" v-model="operator"
                  style="margin-left: 5px;width:160px"></el-input>
        <el-select v-model="type" clearable placeholder="请选择操作类型"  style=" margin-left: 5px;width:160px">
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
       <el-select v-model="status" clearable placeholder="请选择操作状态"  style=" margin-left: 5px;width:160px">
         <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
       </el-select>
      </div>
      <div style="margin-top:8px">
        <el-date-picker v-model="dateRange" type="datetimerange" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00','23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
        <el-button type="primary" style="margin-left: 5px" @click="reset">重置</el-button>
      </div>
    </div>
    <div style="margin:10px 0px">
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除吗？"
          @confirm="handleBatchRemove"
          class="ml-5"
      >
        <el-button type="danger" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定要清空全部操作日志吗？"
          @confirm="handleRemoveAll"
          class="ml-5"
      >
        <el-button type="danger" icon="el-icon-remove-outline" slot="reference">清空全部</el-button>
      </el-popconfirm>
      <el-button type="primary" icon="el-icon-top" style="margin-left: 5px" @click="exp">导出</el-button>
    </div>
    <el-table :data="tableData"
              border stripe
              :header-cell-class-name="headerBg"
              max-height="550"
              v-loading="loading"
              ref="filterTable"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255,225,255, 0.1)"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55px" align="center"></el-table-column>
      <el-table-column prop="id" label="日志编号" width="110px" sortable align="center">
      </el-table-column>
      <el-table-column prop="operation" label="系统模块" width="110px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column prop="type" label="操作类型" width="110px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column prop="operator" label="操作人员" width="110px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column prop="ip" label="操作地址" width="110px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column prop="location" label="操作地点" width="150px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="70px"
                       :filters="[{text:'成功',value:'1'},{text:'失败',value:'0'}]"
                       :filter-method="filterStatus" filter-placement="bottom-end" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '1' ?'success':'danger'">
            {{ scope.row.status === '1' ? '成功' : '失败' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operationTime" label="操作日期" width="150px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column prop="executionTime" label="消耗时间" width="110px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column label="操作" width="210px" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="你确定删除吗？"
              @confirm="handleDelete(scope.row.id)"
              class="ml-5"
          >
            <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
          </el-popconfirm>
          <el-button type="info" icon="el-icon-view el-icon--right" style="margin-left:5px" @click="viewLog(scope.row)">查看</el-button>
          <el-dialog title="操作日志详情" :visible.sync="dialogVisible" width="50%" >
            <el-descriptions size="medium" :column="2">
              <el-descriptions-item ><span slot="label" style="font-weight: bold">操作模块</span>{{log.operation}}/{{log.type}}</el-descriptions-item>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">登录信息</span>{{log.operator}}/{{log.ip}}/{{log.location}}</el-descriptions-item>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">请求方式</span>{{log.requestMethod}}</el-descriptions-item>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">请求地址</span>{{log.operUrl}}</el-descriptions-item>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">操作时间</span>{{log.operationTime}}</el-descriptions-item>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">消耗时间</span>{{log.executionTime}}</el-descriptions-item>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">操作方法</span>{{log.method}}</el-descriptions-item><br/>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">操作状态</span>{{log.status=='1'?'成功':'失败'}}</el-descriptions-item>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">请求参数</span>{{log.operParam}}</el-descriptions-item><br/>
              <el-descriptions-item ><span slot="label" style="font-weight: bold">返回参数</span>{{log.jsonResult}}</el-descriptions-item>
            </el-descriptions>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible= false">关闭</el-button>
            </div>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding:10px 0px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
.headerBgClass {
  background-color: #cccccc !important;
}
</style>

